﻿using LightCAD.Core;
using LightCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightCAD.UI
{
    public partial class ColorItemControl : UserControl
    {
        public event EventHandler<string> ItemClick;
        private string colorName;
        public ColorItemControl()
        {
            InitializeComponent();
        }
        public ColorItemControl(string color)
        {
            InitializeComponent();
            Color sysColor = new Color();
            switch (color) {
                case "ByLayer":
                    sysColor = Color.White;
                    break;
                case "ByBlock":
                    sysColor = Color.White;
                    break;
                case "红":
                    sysColor = Color.Red;
                    break;
                case "黄":
                    sysColor = Color.Yellow;
                    break;
                case "绿":
                    sysColor = Color.Green;
                    break;
                case "青":
                    sysColor = Color.Aqua;
                    break;
                case "蓝":
                    sysColor = Color.Blue;
                    break;
                case "洋红":
                    sysColor = Color.Magenta;
                    break;
                default:
                    break;
            }
            Bitmap bmp = new Bitmap(20, 20);
            Graphics g = Graphics.FromImage(bmp);
            UInt32 uintValue = LayerManageWindow.RGBFromSystem(sysColor);
            SolidBrush b = new SolidBrush(GetColorByUint(uintValue));//这里修改颜色
            g.FillRectangle(b, 0, 0, 20, 20);
            System.Drawing.Rectangle rec = new Rectangle(1, 1, 20, 20);
            g.DrawRectangle(new Pen(System.Drawing.Color.Black, 1), 0, 0, 19, 19);
            this.picColor.Image = bmp;
            this.colorName = color;
            this.lblName.Text = color;

        }

        private void lblName_Click(object sender, EventArgs e)
        {

        }
        public void Reset(uint color, List<LcElement> lcElement)
        {
            if (lcElement != null && lcElement.Count > 0)
            {
                foreach (var element in lcElement)
                {
                    element.Color = color.ToString();
                }
            }
            Bitmap bmp = new Bitmap(20, 20);
            Graphics g = Graphics.FromImage(bmp);
            UInt32 uintValue = Convert.ToUInt32(color);
            SolidBrush b = new SolidBrush(GetColorByUint(uintValue));//这里修改颜色
            g.FillRectangle(b, 0, 0, 20, 20);
            System.Drawing.Rectangle rec = new Rectangle(1, 1, 20, 20);
            g.DrawRectangle(new Pen(System.Drawing.Color.Black, 1), 0, 0, 19, 19);
            this.picColor.Image = bmp;
            this.lblName.Text = colorName;
        }
        private Color GetColorByUint(uint uintValue)
        {
            Color colorConverted = System.Drawing.Color.FromArgb((byte)((uintValue >> 24) & 255), (ushort)((uintValue >> 16) & 255), (ushort)((uintValue >> 8) & 255), (ushort)(uintValue & 255));
            return colorConverted;
        }
    }
}
